import React, { useState } from 'react';
import { Form, Input, Modal, Radio, Upload,Button,message } from 'antd';
import { FormValueType } from '../data.d';
import { validTypeName }  from '@/services/dictype';


const FormItem = Form.Item;

export interface CreateFormProps {
  modalVisible: boolean;
  onSubmit: (values: FormValueType) => void;
  onCancel: () => void;
}

const formLayout = {
  labelCol: { span: 5 },
  wrapperCol: { span: 15 },
};

const CreateForm: React.FC<CreateFormProps> = (props) => {
  const { modalVisible, onSubmit: handleAdd, onCancel } = props;

  const [formVals, setFormVals] = useState<FormValueType>({
    typeName: '',
    typeDescription: ''
  });

  const [form] = Form.useForm();
  const okHandle = async () => {
    const fieldsValue = await form.validateFields();
    form.resetFields();
    handleAdd(fieldsValue);
  };

  const validatorCode = async (_: any, value: string) => {
    const typeid = form.getFieldValue('dicTypeId');
    //先判断和原值有没有变化，有才去服务端验证
    const json = await validTypeName({   typeName: value });
    const promise = Promise;
    if (json && json.msg) {
      return promise.reject(json.msg);
    }
    return promise.resolve();
  };
console.log(formVals)
  return (
    <Modal
      width={640}
      
      destroyOnClose
      title="新增字典类型"
      visible={modalVisible}
      onOk={okHandle}
      onCancel={() => onCancel()}
    >
      <Form
        {...formLayout}
        form={form}
        initialValues={{
          typeName: formVals.typeName,
          typeDescription: formVals.typeDescription
        }}
      >
 
        <FormItem
          label="字典类型名称"
          name="typeName"
          rules={[{ required: true, message: '请输入至少三个字符！', min: 3 }, { validator: validatorCode }]}
        >
          <Input placeholder="请输入" />
        </FormItem>
      
       
        <FormItem
          label="字典类型描述"
          name="typeDescription"
        >
          <Input.TextArea rows={2} placeholder="请输入" />
        </FormItem>
      </Form>
    </Modal>
  );
};

export default CreateForm;
